export function timeFormat(time) {
  let date = new Date()
  date.setTime(time * 1000)
  let y = date.getFullYear()
  let m = toTwo(date.getMonth() + 1)
  let d = toTwo(date.getDate())
  let h = toTwo(date.getHours())
  let mm = toTwo(date.getMinutes())
  let s = toTwo(date.getSeconds())
  return y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s
}

function toTwo(value) {
  if (value < 10) {
    return '0' + value
  }
  return value
}

export function Format (time, fmt) {
  let date = new Date(time)
  // date.setTime(time * 1000)
  const o = {
    'M+': date.getMonth() + 1,
    'D+': date.getDate(),
    'H+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    'S': date.getMilliseconds()
  }
  if (/(Y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}
